'''
Created on 07.04.2012

@author: myaut
'''
import sys

sys.path.append('../../lib')

from salsa.ast import AST
from pycparser import CParser

test = '''  
typedef char int8_t;

typedef unsigned char uint8_t;

typedef char str[128];

typedef union {
    int a;
} u_name;

typedef struct holder {
    int8_t z; 
} holder_t;

typedef void (*func)(int);
'''

macros = {}

parser = CParser(macros_lookup_func = macros.get)
c_ast = parser.parse(test, filename='test.c', debuglevel=9)

ast = AST("C")
ast.from_c_ast('test.c', c_ast)
ast.write('typedef_ast.xml')

ast_root = ast.get_ast_root()

for typedecl in ast_root.iterfind('Typedef'):
    name = typedecl.attrib['name']
    coord = typedecl.attrib.get('line', 0)
    ref = ast.get_node_id(typedecl)
    
    print 'Name: ' + name
    print 'Coord: ' + coord
    print 'Ref: ' + ref
    print